<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>防护知识答题</title>
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        .quiz-container { max-width: 600px; margin: 40px auto; background: #fff; border-radius: 20px; box-shadow: 0 2px 16px #eee; padding: 40px; }
        .btn { padding: 10px 30px; border: none; border-radius: 6px; font-size: 16px; cursor: pointer; }
        .btn-primary { background: #009688; color: #fff; }
        .btn-success { background: #4caf50; color: #fff; }
        .question-block { margin-bottom: 20px; }
        .result { color: #009688; font-weight: bold; margin: 20px 0; }
    </style>
</head>
<body class="bg-gray-50 min-h-screen text-gray-900">
    <!-- 顶部导航栏 -->
    <nav class="bg-white shadow-sm sticky top-0 z-40">
        <div class="max-w-7xl mx-auto flex items-center justify-between px-4 h-16">
            <div class="flex items-center space-x-2">
                <i class="fa-solid fa-shield-alt text-green-600 text-2xl"></i>
                <span class="text-xl font-bold">应急防护小站</span>
            </div>
            <div class="flex items-center space-x-2">
                <a href="index.jsp" class="px-4 py-2<%=(request.getRequestURI().endsWith("index.jsp") ? " text-green-600 border-b-2 border-green-600 font-medium" : " hover:text-green-600")%>" ><i class="fa-solid fa-house mr-1"></i>首页</a>
                <a href="quiz.jsp" class="px-4 py-2<%=(request.getRequestURI().endsWith("quiz.jsp") ? " text-green-600 border-b-2 border-green-600 font-medium" : " hover:text-green-600")%>" ><i class="fa-solid fa-question-circle mr-1"></i>答题功能</a>
                <a href="${pageContext.request.contextPath}/books" class="px-4 py-2<%=(request.getRequestURI().endsWith("books.jsp") ? " text-green-600 border-b-2 border-green-600 font-medium" : " hover:text-green-600")%>" ><i class="fa-solid fa-cart-shopping mr-1"></i>图书购买</a>
                <a href="user-profile.jsp" class="px-4 py-2<%=(request.getRequestURI().endsWith("user-profile.jsp") ? " text-green-600 border-b-2 border-green-600 font-medium" : " hover:text-green-600")%>" ><i class="fa-solid fa-user mr-1"></i>个人信息</a>
                <a href="submit-article.jsp" class="px-4 py-2<%=(request.getRequestURI().endsWith("submit-article.jsp") ? " text-green-600 border-b-2 border-green-600 font-medium" : " hover:text-green-600")%>" ><i class="fa-solid fa-pen-to-square mr-1"></i>我要投稿</a>
                <a href="feedback.jsp" class="px-4 py-2<%=(request.getRequestURI().endsWith("feedback.jsp") ? " text-green-600 border-b-2 border-green-600 font-medium" : " hover:text-green-600")%>" ><i class="fa-solid fa-comment-dots mr-1"></i>问题反馈</a>
            </div>
        </div>
    </nav>
    
    <!-- 主内容区 -->
    <main class="max-w-3xl mx-auto px-4 py-12">
        <div class="quiz-container shadow-xl rounded-2xl bg-white p-8">
            <div class="flex flex-col md:flex-row md:items-center md:justify-between mb-8 gap-4">
                <h2 class="text-3xl font-bold flex items-center text-green-700"><i class="fa-solid fa-question-circle text-green-500 mr-2"></i> 防护知识答题</h2>
                <div class="flex items-center gap-4">
                    <form method="get" action="quiz" onsubmit="if(document.querySelector('.quiz-empty-hint')){showQuizErrorDetail();return false;}" >
                        <button type="submit" class="btn btn-primary px-6 py-2 text-lg shadow hover:bg-green-700 transition">答题</button>
                    </form>
                    <a href="${pageContext.request.contextPath}/wrong-questions" class="btn btn-primary px-6 py-2 text-lg flex items-center gap-1 bg-orange-500 hover:bg-orange-600 text-white rounded shadow"><i class="fa-solid fa-book-open"></i> 错题本</a>
                </div>
            </div>
            <c:choose>
                <c:when test="${empty sessionScope.user}">
                    <div class="text-center text-red-500 text-lg my-8">请先<a href="login.jsp" class="text-green-600 underline hover:text-green-800">登录</a>后再答题。</div>
                </c:when>
                <c:when test="${not empty sessionScope.user && not empty quizList}">
                    <form method="post" action="quiz" class="space-y-8">
                        <c:forEach var="q" items="${quizList}" varStatus="status">
                            <div class="question-block mb-6">
                                <div class="mb-2 text-lg font-semibold text-gray-800 flex items-center">
                                    <span class="inline-block bg-green-100 text-green-700 rounded px-2 py-1 mr-2">第${status.index+1}题</span> ${q.question}
                                    <span class="ml-4 text-xs text-gray-400">[${q.type}]</span>
                                </div>
                                <div class="grid grid-cols-1 md:grid-cols-2 gap-2">
                                    <label class="flex items-center bg-gray-50 rounded px-3 py-2 cursor-pointer border hover:border-green-400 transition">
                                        <input type="radio" name="answer${status.index}" value="A" class="mr-2">A. ${q.optionA}
                                    </label>
                                    <label class="flex items-center bg-gray-50 rounded px-3 py-2 cursor-pointer border hover:border-green-400 transition">
                                        <input type="radio" name="answer${status.index}" value="B" class="mr-2">B. ${q.optionB}
                                    </label>
                                    <label class="flex items-center bg-gray-50 rounded px-3 py-2 cursor-pointer border hover:border-green-400 transition">
                                        <input type="radio" name="answer${status.index}" value="C" class="mr-2">C. ${q.optionC}
                                    </label>
                                    <label class="flex items-center bg-gray-50 rounded px-3 py-2 cursor-pointer border hover:border-green-400 transition">
                                        <input type="radio" name="answer${status.index}" value="D" class="mr-2">D. ${q.optionD}
                                    </label>
                                </div>
                                <input type="hidden" name="qid${status.index}" value="${q.id}"/>
                            </div>
                        </c:forEach>
                        <div class="flex justify-between items-center mt-8">
                            <span class="text-gray-500 text-sm">共${fn:length(quizList)}题</span>
                            <button type="submit" class="btn btn-success text-lg px-8 py-2 shadow hover:bg-green-700 transition">提交答案</button>
                        </div>
                    </form>
                </c:when>
                <c:otherwise>
                    <div class="text-center text-gray-400 text-lg my-12 quiz-empty-hint" style="border:2px solid #e53e3e;background:#fff8f8;border-radius:12px;padding:32px 16px;">
                        题库已全部答完，或暂无题目。<br>请联系管理员补充题库或刷新页面重试。
                        <br><button onclick="showQuizErrorDetail()" class="mt-4 px-4 py-2 bg-red-500 text-white rounded shadow hover:bg-red-600">查看详细原因</button>
                    </div>
                    <c:if test="${not empty resultMsg}">
                        <div class="result text-center text-green-700 text-xl">${resultMsg}</div>
                        <div class="result text-center text-green-600">安全意识评分：<b>${level}</b>（正确率：${rate}%）</div>
                        <c:if test="${not empty detailList}">
                            <div class="mt-8 mb-4 text-xl font-bold text-green-700 flex items-center"><i class="fa-solid fa-clipboard-check mr-2"></i>本次答题结果</div>
                            <ul class="space-y-4">
                                <c:forEach var="detail" items="${detailList}">
                                    <li class="bg-gray-50 border-l-4 ${detail.correct ? 'border-green-500' : 'border-red-500'} p-4 rounded">
                                        <div class="font-semibold text-gray-800 mb-1">${detail.question}</div>
                                        <div class="grid grid-cols-2 md:grid-cols-4 gap-2 text-sm mb-1">
                                            <span>A. ${detail.optionA}</span>
                                            <span>B. ${detail.optionB}</span>
                                            <span>C. ${detail.optionC}</span>
                                            <span>D. ${detail.optionD}</span>
                                        </div>
                                        <div class="text-base">
                                            <span class="${detail.correct ? 'text-green-600' : 'text-red-600'} font-bold">${detail.correct ? '✔ 正确' : '✘ 错误'}</span>
                                            <span class="ml-4">你的答案：<span class="${detail.correct ? 'text-green-600' : 'text-red-600'}">${detail.userAnswer}</span></span>
                                            <span class="ml-4">正确答案：<span class="text-green-600">${detail.correctAnswer}</span></span>
                                        </div>
                                        <div class="text-gray-500 text-xs mt-1">解析：${detail.explanation}</div>
                                    </li>
                                </c:forEach>
                            </ul>
                        </c:if>
                        <c:if test="${not empty wrongList}">
                            <div class="mt-6 text-center text-red-500 font-bold">你答错的题目：</div>
                            <ul class="mt-2 space-y-2">
                                <c:forEach var="fb" items="${wrongList}">
                                    <li class="bg-red-50 border-l-4 border-red-400 p-3 rounded">
                                        <div class="font-semibold text-gray-800">${fb.question}</div>
                                        <div class="text-gray-600 text-sm">你的答案：<span class="text-red-600">${fb.userAnswer}</span>，正确答案：<span class="text-green-600">${fb.correctAnswer}</span></div>
                                        <div class="text-gray-500 text-xs mt-1">解析：${fb.explanation}</div>
                                    </li>
                                </c:forEach>
                            </ul>
                        </c:if>
                    </c:if>
                </c:otherwise>
            </c:choose>
        </div>
    </main>
    
    <!-- 底部信息区 -->
    <footer class="bg-gray-900 text-white py-10 mt-16">
        <div class="max-w-7xl mx-auto px-4">
            <div class="grid grid-cols-1 md:grid-cols-4 gap-8">
                <div>
                    <div class="flex items-center space-x-2 mb-4">
                        <i class="fa-solid fa-shield-alt text-green-400 text-2xl"></i>
                        <span class="text-xl font-bold">应急防护小站</span>
                    </div>
                    <p class="text-gray-400 text-sm mb-4">守护生命安全</p>
                    <div class="flex space-x-4">
                        <a href="#" class="text-gray-400 hover:text-white"><i class="fa-brands fa-weibo"></i></a>
                        <a href="#" class="text-gray-400 hover:text-white"><i class="fa-brands fa-weixin"></i></a>
                        <a href="#" class="text-gray-400 hover:text-white"><i class="fa-brands fa-twitter"></i></a>
                    </div>
                </div>
                <div>
                    <h3 class="text-lg font-medium mb-4">防护分类</h3>
                    <ul class="space-y-2 text-gray-400">
                        <li><a href="disaster-earthquake.jsp" class="hover:text-white">地震防护</a></li>
                        <li><a href="disaster-fire.jsp" class="hover:text-white">火灾防护</a></li>
                        <li><a href="disaster-typhoon.jsp" class="hover:text-white">台风防护</a></li>
                        <li><a href="disaster-flood.jsp" class="hover:text-white">洪水防护</a></li>
                        <li><a href="disaster-extremeheat.jsp" class="hover:text-white">极端天气</a></li>
                    </ul>
                </div>
                <div>
                    <h3 class="text-lg font-medium mb-4">快速链接</h3>
                    <ul class="space-y-2 text-gray-400">
                        <li><a href="#about" class="hover:text-white">关于我们</a></li>
                        <li><a href="submit-article.jsp" class="hover:text-white">我要投稿</a></li>
                        <li><a href="${pageContext.request.contextPath}/books" class="hover:text-white">防护用品</a></li>
                        <li><a href="feedback.jsp" class="hover:text-white">问题反馈</a></li>
                        <li><a href="#" class="hover:text-white">隐私政策</a></li>
                    </ul>
                </div>
                <div>
                    <h3 class="text-lg font-medium mb-4">订阅我们</h3>
                    <p class="text-gray-400 text-sm mb-4">订阅我们的防护周刊，获取最新应急资讯</p>
                    <div class="flex">
                        <input type="email" placeholder="输入您的邮箱" class="px-4 py-2 rounded-l-lg w-full focus:outline-none focus:ring-2 focus:ring-green-400">
                        <button class="bg-green-600 hover:bg-green-700 text-white px-4 rounded-r-lg"><i class="fa-solid fa-paper-plane"></i></button>
                    </div>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-8 pt-8 text-center text-gray-500 text-sm">
                <p>&copy; 2025 应急防护小站. 保留所有权利.</p>
            </div>
        </div>
    </footer>
    <script>
    function showQuizErrorDetail() {
        let msg = '';
        <% if (request.getAttribute("error") != null) { %>
            msg = '<%= request.getAttribute("error") %>';
        <% } else { %>
            msg = '题库无数据、字段不符或数据库连接异常。请联系管理员检查数据库quiz_questions表结构、字段、数据内容，并确保与网站代码字段映射一致。';
        <% } %>
        const modal = document.createElement('div');
        modal.innerHTML = `
          <div id="quiz-error-modal" style="position:fixed;z-index:9999;left:0;top:0;width:100vw;height:100vh;background:rgba(0,0,0,0.3);display:flex;align-items:center;justify-content:center;">
            <div style="background:#fff;padding:32px 24px;border-radius:16px;max-width:90vw;min-width:320px;box-shadow:0 2px 16px #aaa;text-align:center;">
              <div style="font-size:20px;font-weight:bold;color:#e53e3e;margin-bottom:16px;"><i class='fa-solid fa-triangle-exclamation mr-2'></i>题库异常详情</div>
              <div style="color:#444;font-size:16px;white-space:pre-line;">${msg}</div>
              <button id="quiz-error-close-btn" style="margin-top:24px;padding:8px 32px;background:#009688;color:#fff;border:none;border-radius:6px;font-size:16px;cursor:pointer;">关闭</button>
            </div>
          </div>`;
        document.body.appendChild(modal.firstChild);
        document.getElementById('quiz-error-close-btn').onclick = function() {
            document.getElementById('quiz-error-modal').remove();
        };
    }
    </script>
    <c:if test="${not empty error && error == '今日答题次数已用完，请明天再来！'}">
        <script>
        window.addEventListener('DOMContentLoaded', function() {
            setTimeout(function() {
                var resultHtml = '';
                var result1 = document.querySelector('.result.text-center.text-green-700.text-xl');
                var result2 = document.querySelector('.result.text-center.text-green-600');
                if(result1) resultHtml += result1.outerHTML;
                if(result2) resultHtml += result2.outerHTML;
                var modal = document.createElement('div');
                modal.innerHTML =
                  '<div id="quiz-result-modal" style="position:fixed;z-index:9999;left:0;top:0;width:100vw;height:100vh;background:rgba(0,0,0,0.3);display:flex;align-items:center;justify-content:center;">' +
                    '<div style="background:#fff;padding:32px 24px;border-radius:16px;max-width:90vw;min-width:320px;box-shadow:0 2px 16px #aaa;text-align:center;">' +
                      '<div style="font-size:22px;font-weight:bold;color:#009688;margin-bottom:16px;"><i class="fa-solid fa-star mr-2"></i>今日答题次数已用完</div>' +
                      '<div style="color:#444;font-size:18px;white-space:pre-line;">' + resultHtml + '</div>' +
                      '<button id="quiz-result-close-btn" style="margin-top:24px;padding:8px 32px;background:#009688;color:#fff;border:none;border-radius:6px;font-size:16px;cursor:pointer;">关闭</button>' +
                    '</div>' +
                  '</div>';
                document.body.appendChild(modal.firstChild);
                document.getElementById('quiz-result-close-btn').onclick = function() {
                    document.getElementById('quiz-result-modal').remove();
                };
            }, 300);
        });
        </script>
    </c:if>
</body>
</html> 